﻿using AutoMapper;
using ERPSystem.Common.Domains.Material;
using ERPSystem.Common.DTO;
using ERPSystem.Common.Infrastructure.IRepository;
using ERPSystem.Enum.CommonErrorCodes;
using MediatR;
using SqlSugar;
using System.Data;

namespace BasicInformationCommon.API.BasicInformationCommons.Materiales
{
    public class MaterialesSearchHander : IRequestHandler<MaterialesSearchCommand, Result<MaterialBasicfileDTO>>
    {
        private readonly IMapper _mapper;
        private readonly IBaseRepository<Measureunit> measureunitRepository;
        private readonly IBaseRepository<Supplymanner> supplymannerRepository;
        private readonly IBaseRepository<MaterialCascade> materialCascadeRepository;
        private readonly IBaseRepository<MaterialInformation> materialInformationRepository;

        public MaterialesSearchHander(IMapper mapper, IBaseRepository<Measureunit> measureunitRepository, IBaseRepository<Supplymanner> supplymannerRepository, IBaseRepository<MaterialCascade> materialCascadeRepository, IBaseRepository<MaterialInformation> materialInformationRepository)
        {
            this. _mapper = mapper;
            this.measureunitRepository = measureunitRepository;
            this.supplymannerRepository = supplymannerRepository;
            this.materialCascadeRepository = materialCascadeRepository;
            this.materialInformationRepository = materialInformationRepository;
        }



        /// <summary>
        /// 处理物料基本信息表查询请求
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result<MaterialBasicfileDTO>> Handle(MaterialesSearchCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<MaterialBasicfileDTO>() { Code = (int)CommonErrorCode.Search };
            try
            {
                var measureunit = await (await measureunitRepository.GetAll()).ToListAsync();//计量单位
                var supplymanner = await (await supplymannerRepository.GetAll()).ToListAsync();//供应方式
                var materialCascade = await (await materialCascadeRepository.GetAll()).ToListAsync();//物料分类
                var materialInformation = await (await materialInformationRepository.GetAll()).ToListAsync();//物料基本档

                //var aa = (from a in materialCascade
                //          join b in measureunit on a.MeasureId equals b.MeasureId
                //          join c in supplymanner on a.SupplyId equals c.SupplyId
                //          join d in materialInformation on a.MaterialTypeId equals d.MaterialTypeId
                //          where a.MaterialTypeId == request.MaterialTypeId
                //          select new MaterialBasicfileDTO()
                //          {
                //              MaterialTypeId = a.MaterialTypeId,
                //              MeasureId = b.MeasureId,
                //              MeasureName = b.MeasureName,
                //              SupplyId = c.SupplyId,
                //              SupplyName = c.SupplyName,
                //              ItemBianma = d.ItemBianma,
                //              ItemName = d.ItemName,
                //              Modelspecification = d.Modelspecification,
                //              Drawingnumber = d.Drawingnumber
                //          }).ToList();

                var aa = (from a in materialInformation
                          join b in supplymanner on a.SupplyId equals b.SupplyId
                          join c in measureunit on a.MeasureId equals c.MeasureId
                          select new MaterialBasicfileDTO()
                          {
                              MaterialTypeId = a.MaterialTypeId,
                              MeasureId = a.MeasureId,
                              MeasureName = c.MeasureName,
                              SupplyId = b.SupplyId,
                              SupplyName = b.SupplyName,
                              ItemBianma = a.ItemBianma,
                              ItemName = a.ItemName,
                              Modelspecification = a.Modelspecification,
                              Drawingnumber = a.Drawingnumber
                          }).ToList();

                if (request.MaterialTypeId != 0)
                {
                    aa = aa.Where(x => x.MaterialTypeId == request.MaterialTypeId).ToList();
                }

                if (!string.IsNullOrEmpty(request.ItemBianma))
                {
                    aa = aa.Where(x => x.ItemBianma == request.ItemBianma).ToList();
                }
                if (!string.IsNullOrEmpty(request.ItemName))
                {
                    aa = aa.Where(x => x.ItemName.Contains(request.ItemName)).ToList();
                }
                result.Data2 = aa;

                
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

    }
}
